package com.example.business.service.impl;

import com.example.business.dao.entity.Plan;
import com.example.business.dao.mapper.ArticleDetailsMapper;
import com.example.business.dao.mapper.PlanMapper;
import com.example.business.service.PlanService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class PlanServiceImpl implements PlanService {
    private final PlanMapper planMapper;
    private final ArticleDetailsMapper articleDetailsMapper;
    @Override
    public void savePlan(String planContent) {
        try {
            LocalDate today = LocalDate.now();
            // 1. 查询当天是否已有计划
            Plan existingPlan = planMapper.selectByRecordDate(today);

            Plan plan = new Plan();
            plan.setContent(planContent);
            plan.setRecordDate(today);

            int taskCount = planContent.split("\n").length; // 统计任务数量
            if (existingPlan != null) {
                // 2. 已有计划则更新
                plan.setId(existingPlan.getId());
                planMapper.updateById(plan);
            } else {
                // 3. 无计划则新增
                planMapper.insert(plan);
            }
        } catch (Exception e) {
            log.error("保存计划失败", e);
        }
    }

    @Override
    public Plan getPlan() {
        LocalDate today = LocalDate.now();
        return planMapper.selectByRecordDate(today);
    }

    @Override
    public List<String> getPlanList() {
        return null;
    }
}
